package com.android.test.util;

import java.lang.reflect.Method;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.provider.Settings.Secure;

public class DeviceUtil {

    public static final int SDK_VERSION_1_5 = 3;

    public static final int SDK_VERSION_1_6 = 4;

    public static final int SDK_VERSION_2_0 = 5;

    public static final int SDK_VERSION_2_0_1 = 6;

    public static final int SDK_VERSION_2_1 = 7;

    public static final int SDK_VERSION_2_2 = 8;

    public static final int SDK_VERSION_2_3 = 9;

    public static final int SDK_VERSION_2_3_3 = 10;

    public static final int SDK_VERSION_3_0 = 11;

    public static String getDeviceModel() {
	return StringUtil.makeSafe(Build.MODEL);
    }

    public static String getReleaseVersion() {
	return StringUtil.makeSafe(Build.VERSION.RELEASE);
    }

    /**
     * 获得国际移动设备身份码
     * 
     * @param context
     * @return
     */
    public static String getIMEI(Context context) {
	return ((TelephonyManager) context
		.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    /**
     * 获得MSISDN
     * 
     * @param context
     * @return
     */
    public static String getMSISDN(Context context) {
	if (context == null)
	    return "NoNumber";
	String msisdn = ((TelephonyManager) context
		.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
	if (msisdn == null)
	    return "NoNumber";
	return msisdn;
    }

    /**
     * 获得国际移动用户识别码
     * 
     * @param context
     * @return
     */
    public static String getIMSI(Context context) {
	return ((TelephonyManager) context
		.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
    }

    /**
     * 获得设备屏幕矩形区域范围
     * 
     * @param context
     * @return
     */
    public static Rect getScreenRect(Context context) {
	Display display = ((WindowManager) context
		.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
	int w = display.getWidth();
	int h = display.getHeight();
	return new Rect(0, 0, w, h);
    }

    /**
     * 获得设备屏幕密度
     */
    public static float getScreenDensity(Context context) {
	DisplayMetrics metrics = context.getApplicationContext().getResources()
		.getDisplayMetrics();
	return metrics.density;
    }

    public static int getScreenDensityDpi(Context context) {
	DisplayMetrics metrics = context.getApplicationContext().getResources()
		.getDisplayMetrics();
	return (int) (metrics.density * 160);
    }

    /**
     * 获得系统版本
     */
    public static String getSDKVersion() {
	return android.os.Build.VERSION.SDK;
    }

    public static int getSDKVersionInt() {
	try {
	    return Integer.valueOf(android.os.Build.VERSION.SDK);
	} catch (Exception e) {
	    return 0;
	}
	// return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * deviceId = IMEI_androidId jiangxb 2011/05/28
     */

    /**
     * 获得android_id
     * 
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
	return Secure
		.getString(context.getContentResolver(), Secure.ANDROID_ID);
    }

    /**
     * 获得deviceId
     * 
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
	// return getIMEI(context) + "_" + getAndroidId(context);
	return getIMEI(context);
    }

    /**
     * 获得屏幕尺寸
     * 
     * @param context
     * @return
     */
    public static String getResolution(Context context) {
	Rect rect = getScreenRect(context);
	return rect.right + "x" + rect.bottom;
    }

    public static String getSerialNumber() {
	String serialNumber = "";

	try {
	    Class<?> c = Class.forName("android.os.SystemProperties");
	    Method get = c.getMethod("get", String.class);
	    serialNumber = (String) get.invoke(c, "ro.serialno");

	    if (serialNumber.equals("")) {
		serialNumber = "?";
	    }
	} catch (Exception e) {
	    if (serialNumber.equals("")) {
		serialNumber = "?";
	    }
	}

	return serialNumber;
    }

}